<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
	<title>LuaJava - A Script Tool for Java</title>
    <link rel="stylesheet" href="http://www.keplerproject.org/doc.css" type="text/css"/>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>

<body>

<div id="container">

<div id="product">
	<div id="product_logo"><a href="http://www.keplerproject.org">
		<img alt="LuaJava" src="luajava_128.png"/>
	</a></div>
	<div id="product_name"><big><b>LuaJava</b></big></div>
	<div id="product_description">A Script Tool for Java</div>
</div> <!-- id="product" -->

<div id="main">
<div id="navigation">
	
<h1>LuaJava</h1>
	<ul>
		<li><a href="index.html">Home</a>
			<ul>
				<li><a href="index.html#overview">Overview</a></li>
				<li><a href="index.html#status">Status</a></li>
				<li><a href="index.html#dependencies">Dependencies</a></li>
				<li><a href="index.html#download">Download</a></li>
				<li><a href="index.html#credits">Credits</a></li>
				<li><a href="index.html#contact">Contact us</a></li>
			</ul>
		</li>
		<li><a href="manual.html">Manual</a>
			<ul>
				<li><a href="manual.html#compile">Compiling</a></li>
				<li><a href="manual.html#install">Installing</a></li>
				<li><a href="manual.html#console">Java Console</a></li>
				<li><a href="manual.html#luareference">Lua Reference</a></li>
				<li><a href="manual.html#javareference">Java Reference</a></li>
			</ul>
		</li>
		<li><strong>Examples</strong></li>
		<li><a href="history.html">History</a></li>
    <li><a href="http://luaforge.net/projects/luajava/">Project</a>
       <ul>
          <li><a href="http://luaforge.net/tracker/?group_id=10">Bug Tracker</a></li>
          <li><a href="http://luaforge.net/scm/?group_id=10">CVS</a></li>
       </ul>
    </li>
		<li><a href="license.html">License</a></li>
	</ul>
</div> <!-- id="navigation" -->

<div id="content">

<h2>Examples</h2>

<h4>Hello World using LuaJava</h4>

<p>This example is a simple Hello World application using LuaJava printing both from Lua and Java.</p>

<p><code>Hello.java</code></p>
<pre class="example">public class Hello
{
  public static void main(String[] args)
  {
    LuaState L = LuaStateFactory.newLuaState();
    L.openLibs();
    
    L.LdoFile("hello.lua");
    
    System.out.println("Hello World from Java!");
  }
}
</pre>

<p><code>hello.lua</code></p>

<pre class="example">print("Hello World from Lua!")
</pre>

<h4>Accessing a DataBase</h4>

<p>This example uses Java JDBC to access a database from Lua.</p>

<p><code>TestJDBC.java</code></p>
<pre class="example">public class TestJDBC
{
  
  public static void main(String[] args) throws Exception
  {
    // gets a java.sql.Connection and creates a Statement

    Class.forName("org.hsqldb.jdbcDriver");
    Connection con = DriverManager.getConnection(
	                    "jdbc:hsqldb:hsql://localhost:9002",
	                    "sa", "");
    Statement st = con.createStatement();
    
    try
    {
      st.execute("DROP TABLE luatest");
    }
    catch (Exception ignore) {}
    
    st.execute([[CREATE TABLE luatest 
               (id int not null primary key, 
               str varchar, number double)]]);
    
    for(int i = 0 ; i &lt; 10 ; i++)
    {
      st.executeUpdate(
          "INSERT INTO luatest (id, str, number) values(" + 
          i + ", '" + 2*i + "', " + 
          System.currentTimeMillis() + ")");
    }
    
    // creates a Lua State and register the basic libraries
    
    LuaState L = LuaStateFactory.newLuaState();
    L.openLibs();
    
    // registers the statement in the global 'st'
    
    L.pushObjectValue(st);
    L.setGlobal("st");
    
    // execute file testJDBC.lua
    
    int err = L.LdoFile("testJDBC.lua");

    L.close();
    st.close();
    con.close();
  }
}
</pre>


<p><code>testJDBC.lua</code></p>
<pre class="example">do
-- tests if object Statement is nil
if st == nil then
    print("Error. st object is nil")
    return
end

local st = st
_G.st = nil

function createSQLProxy(t)

    local tableName = t
    
    local function i (t,k)
    
        local id = tonumber(k)
        if not id then
            return nil
        end
        
        local function mi (t,k)
        
            local sql = "select "..k.." from "..
                        tableName.." where id="..id
            local rs = st:executeQuery(sql)
            if not rs:next() then
                rs:close()
                return nil
            end
            
            local res = rs:getString(1)
            rs:close()
            
            return res
        end
        
        local res = {}
        setmetatable(res, {__index = mi})
        return res
    end
    
    local res = {}
    setmetatable(res, {__index = i })
    return res
end

end


proxy = createSQLProxy("luatest")

print(proxy[1].str)
print(proxy[1].number)

</pre>

</div> <!-- id="content" -->

</div> <!-- id="main" -->


<div id="about">
	<p><a href="http://validator.w3.org/check?uri=referer">
    <img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" /></a></p>
      <p><small>$Id: examples.html,v 1.17 2007-01-23 22:37:28 thiago Exp $</small></p>
</div> <!-- id="about" -->

</div> <!-- id="container" -->

</body>
</html> 
